package com.manage.project.biz.controller;

import com.manage.common.utils.poi.ExcelUtil;
import com.manage.framework.aspectj.lang.annotation.Log;
import com.manage.framework.aspectj.lang.enums.BusinessType;
import com.manage.framework.web.controller.BaseController;
import com.manage.framework.web.domain.AjaxResult;
import com.manage.framework.web.page.TableDataInfo;
import com.manage.project.biz.domain.*;
import com.manage.project.biz.service.IBizGongluService;
import com.manage.project.biz.service.IBizSuidaoService;
import com.manage.project.biz.service.ICivilEnginneeringCleaningService;
import com.manage.project.biz.service.ICivilEnginneeringFacilityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
//import com.ruoyi.common.utils.poi.ExcelUtil;
//import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 清洁Controller
 *
 * @author ycd
 * @date 2022-05-30
 */
@RestController
@RequestMapping("/biz/cleaning")
public class CivilEnginneeringCleaningController extends BaseController
{
    @Autowired
    private ICivilEnginneeringCleaningService civilEnginneeringCleaningService;
    @Autowired
    private IBizGongluService roadInfoService;
    @Autowired
    private IBizSuidaoService iBizSuidaoService;
    @Autowired
    private ICivilEnginneeringFacilityService iCivilEnginneeringFacilityService;
    /**
     * 查询清洁列表
     */
    @PreAuthorize("@ss.hasPermi('biz:cleaning:list')")
    @GetMapping("/list")
    public TableDataInfo list(CivilEnginneeringCleaning civilEnginneeringCleaning)
    {
        startPage();
        List<CivilEnginneeringCleaning> list = civilEnginneeringCleaningService.selectCivilEnginneeringCleaningList(civilEnginneeringCleaning);
        return getDataTable(list);
    }

    /**
     * 导出清洁列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:cleaning:export')")
//    @Log(title = "清洁", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, CivilEnginneeringCleaning civilEnginneeringCleaning) throws IOException {
//        List<CivilEnginneeringCleaning> list = civilEnginneeringCleaningService.selectCivilEnginneeringCleaningList(civilEnginneeringCleaning);
//        ExcelUtil<CivilEnginneeringCleaning> util = new ExcelUtil<CivilEnginneeringCleaning>(CivilEnginneeringCleaning.class);
//        util.exportExcel(response, list, "清洁数据");
//    }
//    @PreAuthorize("@ss.hasPermi('biz:cleaning:export')")
    @Log(title = "清洁", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CivilEnginneeringCleaning civilEnginneeringCleaning) throws IOException {
        List<CivilEnginneeringCleaning> list = civilEnginneeringCleaningService.selectCivilEnginneeringCleaningList(civilEnginneeringCleaning);
        ExcelUtil<CivilEnginneeringCleaning> util = new ExcelUtil<CivilEnginneeringCleaning>(CivilEnginneeringCleaning.class);
        return util.exportExcel( list, "清洁数据");
    }


    /**
     * 获取清洁详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:cleaning:query')")
    @GetMapping(value = "/{tCleanId}")
    public AjaxResult getInfo(@PathVariable("tCleanId") Long tCleanId)
    {
        return AjaxResult.success(civilEnginneeringCleaningService.selectCivilEnginneeringCleaningByTCleanId(tCleanId));
    }

    /**
     * 新增清洁
     */
    @PreAuthorize("@ss.hasPermi('biz:cleaning:add')")
    @Log(title = "清洁", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CivilEnginneeringCleaning civilEnginneeringCleaning)
    {
        String roadName=civilEnginneeringCleaning.getRoadName();
        String tunnelName=civilEnginneeringCleaning.getTunnelName();
        String tunnelId=civilEnginneeringCleaning.getTunnelId();
        String facility=civilEnginneeringCleaning.getFacilityName();
        String mileageNumber=civilEnginneeringCleaning.getMileageNumber();
        BizSuidao bizSuidao= iBizSuidaoService.getByTunnelNameIdAndRoadName(roadName,tunnelName,tunnelId);
        if(bizSuidao==null){
            return new AjaxResult().error("当前公路名称、隧道名称和隧道编码不对应，请核对后重新输入");
        }
        CivilEnginneeringFacility civilEnginneeringFacility=iCivilEnginneeringFacilityService.getByNameAndMileageNumber(tunnelName,facility,mileageNumber);
        if(civilEnginneeringFacility==null){
            return new AjaxResult().error(tunnelName+mileageNumber+"里程桩号位置未安装"+facility+",请联系管理员");
        }
        BizGonglu roadInfo=roadInfoService.checkByRoadName(roadName);
        if(roadInfo==null) {
            return new AjaxResult().error("当前道路不存在，请输入正确的道路名称");
        }else {
            return toAjax(civilEnginneeringCleaningService.insertCivilEnginneeringCleaning(civilEnginneeringCleaning));
        }
    }

    /**
     * 修改清洁
     */
    @PreAuthorize("@ss.hasPermi('biz:cleaning:edit')")
    @Log(title = "清洁", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CivilEnginneeringCleaning civilEnginneeringCleaning)
    {
        String roadName=civilEnginneeringCleaning.getRoadName();
        String tunnelName=civilEnginneeringCleaning.getTunnelName();
        String tunnelId=civilEnginneeringCleaning.getTunnelId();
        String facility=civilEnginneeringCleaning.getFacilityName();
        String mileageNumber=civilEnginneeringCleaning.getMileageNumber();
        BizSuidao bizSuidao= iBizSuidaoService.getByTunnelNameIdAndRoadName(roadName,tunnelName,tunnelId);
        if(bizSuidao==null){
            return new AjaxResult().error("当前公路名称、隧道名称和隧道编码不对应，请核对后重新输入");
        }
        CivilEnginneeringFacility civilEnginneeringFacility=iCivilEnginneeringFacilityService.getByNameAndMileageNumber(tunnelName,facility,mileageNumber);
        if(civilEnginneeringFacility==null){
            return new AjaxResult().error(tunnelName+mileageNumber+"里程桩号位置未安装"+facility+",请联系管理员");
        }
        BizGonglu roadInfo=roadInfoService.checkByRoadName(roadName);
        if(roadInfo==null) {
            return new AjaxResult().error("当前道路不存在，请输入正确的道路名称");
        }else {
            return toAjax(civilEnginneeringCleaningService.updateCivilEnginneeringCleaning(civilEnginneeringCleaning));
        }
    }

    /**
     * 删除清洁
     */
    @PreAuthorize("@ss.hasPermi('biz:cleaning:remove')")
    @Log(title = "清洁", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tCleanIds}")
    public AjaxResult remove(@PathVariable Long[] tCleanIds)
    {
        return toAjax(civilEnginneeringCleaningService.deleteCivilEnginneeringCleaningByTCleanIds(tCleanIds));
    }
}
